package com.mew.chess.core.game.qmcs.game.task;


import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.ChessSessionManager;
import com.mew.chess.core.game.qmcs.game.QmcsGameManager;
import com.mew.chess.core.game.qmcs.game.QmcsTableManager;
import com.mew.chess.core.game.qmcs.game.common.QmcsBO;
import com.mew.chess.core.game.qmcs.game.common.QmcsConstantValue;
import com.mew.chess.core.game.qmcs.game.common.QmcsLinkType;
import com.mew.chess.core.game.qmcs.game.common.QmcsRoute;
import com.mew.chess.core.game.qmcs.game.message.QmcsCMDMapType;
import com.mew.chess.core.game.qmcs.game.message.QmcsPushMessage;
import com.mew.chess.core.game.qmcs.service.QmcsService;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.util.NumberUtil;
import com.zebra.common.concurrent.ZebraScheduler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
public class QmcsPushCycleScheduleTask {

    @Autowired
    private QmcsService qmcsService;

    public void fishCycle(int roomId, int table,ScheduledFuture<?> stepFuture){

        QmcsTableManager tableManager = QmcsGameManager.getInstance().getController(roomId).getTable(table);
        if (tableManager.getLinkType()!= QmcsLinkType.Fish.code()){
            if (null != stepFuture){
                ZebraScheduler.cancelScheduled(stepFuture);
            }
            return;
        }

        tableManager.deadFish();

        //普通
        int rand = NumberUtil.randomIntByStr(QmcsConstantValue.FISH_NUMBER, "-");
        List<QmcsRoute> fishQmcsRoute = QmcsGameManager.getInstance().getQmcsRoutes();
        HashSet<Integer> ids = new HashSet<>();
        do {
            ids.add(RandomUtil.randomInt(fishQmcsRoute.size()));
        }while (ids.size()<rand);

        QmcsBO[] qmcsBOS = creatFish(tableManager,ids);

        sendStartMsg(tableManager, qmcsBOS);

        qmcsService.tableLog(tableManager,false);
    }

    private QmcsBO[] creatFish(QmcsTableManager tableManager, HashSet<Integer> ids) {
        List<QmcsRoute> fishQmcsRoute = QmcsGameManager.getInstance().getQmcsRoutes();

        ArrayList<QmcsBO> qmcsBOS = new ArrayList<>();
        for (Integer id : ids) {
            QmcsRoute qmcsRoute = fishQmcsRoute.get(id);
            int andIncrement = tableManager.getFishKeyId().getAndIncrement();
            if (andIncrement>99){
                tableManager.getFishKeyId().set(1);
            }
            QmcsBO qmcsBO = QmcsBO.creat(andIncrement, qmcsRoute);
            tableManager.getQmcsMap().put(qmcsBO.getFishId(), qmcsBO);
            qmcsBOS.add(qmcsBO);
        }

        return qmcsBOS.toArray(new QmcsBO[qmcsBOS.size()]);
    }

    private void sendStartMsg(QmcsTableManager tableManager, QmcsBO[] qmcsBOS){
        QmcsPushMessage message = QmcsPushMessage.builder().qmcsBOS(qmcsBOS).build();
        tableManager.getOnlineUserMap().forEach((k, v) -> {
            GameSession session = v.getGameSession();
            ChessPushMessageUtil.pushMessage(session,QmcsCMDMapType.QmcsPushMessage.code(),message);

        });
    }
}
